//
//  DGNRankTableCell.swift
//  Digimon
//
//  Created by Vill on 2023/10/16.
//

import UIKit

class DGNRankTableCell: UITableViewCell {
    
    // 序号
    @IBOutlet weak var numLbl: UILabel!
    // 排序logo
    @IBOutlet weak var leftImg: UIImageView!
    // 头像
    @IBOutlet weak var iconImg: UIImageView!
    // 关注按钮
    @IBOutlet weak var followBtn: UIButton!
    // 昵称
    @IBOutlet weak var nameLbl: UILabel!
    // 热门
    @IBOutlet weak var hotCoinLbl: UILabel!
    
    static let reusedID = "DGNRankTableCell"
    public func cellWithTableView(_ tableView:UITableView) -> DGNRankTableCell{
        var cell:DGNRankTableCell! = tableView.dequeueReusableCell(withIdentifier: DGNRankTableCell.reusedID) as? DGNRankTableCell
        if cell == nil {
            cell = (Bundle.main.loadNibNamed("DGNRankTableCell", owner: nil, options: nil)?.first as! DGNRankTableCell)
        }
        return cell!
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.iconImg.layer.cornerRadius = PTW(25)
        self.iconImg.contentMode = .scaleAspectFill
        self.iconImg.layer.masksToBounds = true
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    
    var model:CORankModel?{
        didSet {
            guard let model = model else {
                return
            }
            self.iconImg.sd_setUrlImage(model.imgUrl, placeholder: KImaged("coco_icon_holder"))
            self.nameLbl.text = model.streamerName
            self.hotCoinLbl.text = "🔥 \(model.addCoins) Fans"
            //是否关注、0否。大于0已关注
            self.followBtn.isSelected = model.isfollow > 0
        }
    }
    
    @IBAction func followAction(_ sender: UIButton) {
        var params = [String:Any]()
        params["streamerId"]  = self.model?.streamerId
        if (self.model?.isfollow ?? 0 > 0){
            // 取消关注
            COHttpManager.request(target: COBaseApi.streamerUnFollow(param: params), modelType: CONSObjectModel.self) {[weak self] code, object, success in
                self?.model?.isfollow = 0
                sender.isSelected = self?.model?.isfollow ?? 0 > 0
                self?.viewController()?.view.makeToast("Sussessful!")
            } failureBlock: { [weak self] code, msg in
                self?.viewController()?.view.makeToast("Network error,Please try again later!")
            }
        }else{
            // 关注
            COHttpManager.request(target: COBaseApi.streamerFollow(param: params), modelType: CONSObjectModel.self) {[weak self] code, object, success in
                self?.model?.isfollow = 1
                sender.isSelected = self?.model?.isfollow ?? 0 > 0
                self?.viewController()?.view.makeToast("Sussessful!")
            } failureBlock: {[weak self] code, msg in
                self?.viewController()?.view.makeToast("Network error,Please try again later!")
            }
        }
    }
    
    
    
}
